package io.renren.modules.score.service.impl;

import io.renren.modules.score.entity.CountWeight;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.score.dao.ScoreDao;
import io.renren.modules.score.entity.ScoreEntity;
import io.renren.modules.score.service.ScoreService;


@Service("scoreService")
public class ScoreServiceImpl extends ServiceImpl<ScoreDao, ScoreEntity> implements ScoreService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int userid=Integer.valueOf((String)params.get("key"));
        IPage<ScoreEntity> page;
        if(userid!=1) {
            page = this.page(
                    new Query<ScoreEntity>().getPage(params),
                    new QueryWrapper<ScoreEntity>().eq("userid",userid)
            );
        }else {
             page = this.page(
                    new Query<ScoreEntity>().getPage(params),
                    new QueryWrapper<ScoreEntity>()
            );
        }
        return new PageUtils(page);
    }
    @Override
    public double getCount(int [] countvo){
        double [] first={1.0};
        // 构建第一级
        List<CountWeight> countWeightFirstList =new ArrayList<>();
        for(int i=0;i<first.length;i++){
        CountWeight countWeight =new CountWeight();
        countWeight.setWeight(first[i]);
        countWeightFirstList.add(countWeight);
        }

        // 构建第二级
        double [] second={0.5,0.2,0.3};
        for(int i=0;i<second.length;i++){
            CountWeight countWeight =new CountWeight();
            countWeight.setWeight(second[i]);
            countWeightFirstList.get(0).getChild().add(countWeight);
        }

        // 构建第三级
        for(int i=0;i<second.length;i++){
            CountWeight countWeight =new CountWeight();
            countWeight.setWeight(second[i]);
            countWeightFirstList.get(0).getChild().get(0).getChild().add(countWeight);
        }
        for(int i=0;i<second.length;i++){
            CountWeight countWeight =new CountWeight();
            countWeight.setWeight(second[i]);
            countWeightFirstList.get(0).getChild().get(1).getChild().add(countWeight);
        }
        for(int i=0;i<second.length;i++){
            CountWeight countWeight =new CountWeight();
            countWeight.setWeight(second[i]);
            countWeightFirstList.get(0).getChild().get(2).getChild().add(countWeight);
        }

        // 把每一项的权重算出来
       double [] weight = new double[countvo.length];
        int number=0;
        for(int i=0;i<countWeightFirstList.size();i++){
            for(int j=0;j<countWeightFirstList.get(i).getChild().size();j++){
                for(int k=0;k<countWeightFirstList.get(i).getChild().get(j).getChild().size();k++){
                    // 最后一级的权重是等于每一级权重相乘
                    weight[number]=
                            countWeightFirstList.get(i)
                                    .getChild().get(j)
                                    .getChild().get(k).getWeight()*countWeightFirstList.get(i)
                                    .getChild().get(j).getWeight()*countWeightFirstList.get(i).getWeight();
                    number++;
                }
            }
        }
        // 计算分数
        double score=0;
        double weights=0.0;
        for(int i=0;i<weight.length-1;i++){
            score=score+weight[i]*countvo[i];
            weights=weight[i]+weights;
        }
        // 保证权重最后为1
         score =score+countvo[weight.length-1]*(1-weights);
        System.out.println(Arrays.toString(weight));
        return score;
    }
    @Override
    public  ScoreEntity getscore(int id){
        ScoreEntity scoreEntity= this.getOne(new QueryWrapper<ScoreEntity>().eq("userid",id));
        return scoreEntity;
    };
}
